package com.company.bit;

public class HammingWeight {
    public int hammingWeight(int n) {
//        int count = 0;
//        String binaryString = Integer.toBinaryString(n);
//        for (int i = 0; i < binaryString.length(); i++) {
//            if (binaryString.charAt(i) == '1') {
//                count++;
//            }
//        }

        return divTwo(n);
    }

    private int divTwo(int n) {
        int count = 0;
        int num = n;
        while (num / 2 != 0) {
            System.out.println("--------  ");
            System.out.println("num = "+num);
            System.out.println("--------  ");
            count++;
            num = num/2;
        }
        count+=num%2;
        return count;
    }
}
